package org.light.reflecttable.database;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
/**
 * DBConf must owned by the Project Manager
 * You can change the configure to fit your situation.
 * 
 * This class should not change others except the databasename,
 * username and password
 * 
 * @author Jerry Shen
 * @release under GPLv2
 * @email jerry_shen_sjf@qq.com
 *
 */
public final class DBConf{
    private static Connection connection = null;

	public static void setConnection(Connection connection) {
		DBConf.connection = connection;
	}
	
	public synchronized static Connection initDB(String dbHost, String dbPort, String dbName, String userName, String password) throws Exception{
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://"+dbHost+":"+dbPort+"/" + dbName + "?useUnicode=true&characterEncoding=utf-8";
        	connection = DriverManager.getConnection(url,userName,password);
        } catch (Exception e){
        	return null;
        }
        return connection;
    }
	
	public synchronized static Connection initMariaMetaDB(String dbHost, String dbPort, String dbName, String userName, String password) throws Exception{
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://"+dbHost+":"+dbPort+"/?useUnicode=true&characterEncoding=utf-8";
        	connection = DriverManager.getConnection(url,userName,password);
        } catch (Exception e){
        	return null;
        }
        return connection;
    }
	
	public synchronized static Connection initPgDB(String dbHost, String dbPort, String dbName, String userName, String password) throws Exception{
        try {
            Class.forName("org.postgresql.Driver");
            String url = "jdbc:postgresql://"+dbHost+":"+dbPort+"/"+dbName;
        	connection = DriverManager.getConnection(url,userName,password);
        } catch (Exception e){
        	return null;
        }
        return connection;
    }
	
	public synchronized static Connection initPgMetaDB(String dbHost, String dbPort, String dbName, String userName, String password) throws Exception{
        try {
            Class.forName("org.postgresql.Driver");
            String url = "jdbc:postgresql://"+dbHost+":"+dbPort+"/";
        	connection = DriverManager.getConnection(url,userName,password);
        } catch (Exception e){
        	return null;
        }
        return connection;
    }
}

